/*** 
 * @Date: 2020-07-10 16:06:01
 * @LastEditors: leehave
 * @LastEditTime: 2020-08-21 11:55:39
 */
import staticPath from "@/assets/json/index";
import router from '@/router/index';
import { saveStorage, loadStorage } from '@/utils/storage'
import { parseAsync } from "@babel/core";
import { appInfo } from '@/service/app';
import { bannerService } from '@/service/user';
import { Toast } from "vant";
let windowUrl =  process.env.VUE_APP_HOST || window.location.host
const app = {
  namespaced: true,
  state: {
    tabList: [
      {
        name: "首页",
        icon: staticPath.tab[0].icon,
        activeIcon: staticPath.tab[0].activeIcon,
        path: "/home",
        id: 0,
        active: true
      },
      {
        name: "护理上门",
        icon: staticPath.tab[1].icon,
        activeIcon: staticPath.tab[1].activeIcon,
        path: "/huli",
        id: 1,
        active: false
      },
      {
        name: "个人中心",
        icon: staticPath.tab[2].icon,
        activeIcon: staticPath.tab[2].activeIcon,
        path: "/user/index",
        id: 2,
        active: false
      }
    ],
    url: windowUrl,
    appInfo: '' || loadStorage('appInfo'),
    bannerList: {
      home: [],
      homehuli: [],
      news:[],
      huli:[],
      subscribe: []
    }
  },
  mutations: {
    setBanner: (state, params) => {
      let bannerList = {
        home: params[0].data,
        homehuli: params[1].data,
        news: params[2].data,
        huli: params[3].data,
        subscribe: params[4].data,
      }
      state.bannerList = bannerList
    },
    tabActive: (state, params) => {
      let tabList = Object.assign([],state.tabList);
      tabList.map(item => {
        item.active = false;
        if(item.id == params.id){
          item.active = true
        }
      })
      router.push({
        path: params.path
      })
      state.tabList = tabList;
    },
    setChargeBalanceText: (state, params) => {
      state.tabList[1].name = params
    },
    regexTabActive: (state, params) => {
      let tabList = Object.assign([],state.tabList);
      tabList.map(item => {
        item.active = false;
        if(item.id == params.id){
          item.active = true
        }
      })
      // router.push({
      //   path: params.path
      // })
      state.tabList = tabList;
    },

    /**
     * 存储全局信息
     */
    setAppInfo: (state, params) => {
      state.appInfo = params
    }
  },
  actions: {
    setAppInfoAction({commit}, params){
      appInfo().then(res => {
        let appinfo = {}
        res.data.map(item => {
          let obj = {}
          obj = {
            'cname': item.cname,
            'title': item.title,
            'value': item.value
          }
          appinfo[item.title] = obj
        })
        commit('setAppInfo', appinfo)
        saveStorage('appInfo', appinfo)
      })
    },
    setBannerAction({commit},params){
      Promise.all([bannerService(1),bannerService(2),bannerService(3),bannerService(4),bannerService(5)]).then(result => {
        console.log(result,'setBannerAction')
        commit('setBanner', result)
        saveStorage('bannerList', result)
      })
    }
  }
};
export default app;